package com.sacc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.power.common.model.CommonResult;
import com.sacc.mapper.BoxAllMapper;
import com.sacc.mapper.BoxCollectedMapper;
import com.sacc.pojo.BoxCollected;
import com.sacc.service.BoxCollectedService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 滚~韬
 * @date 2022/4/16 15:47
 */
@Service
public class BoxCollectedServiceImpl implements BoxCollectedService {
    @Autowired
    private BoxCollectedMapper boxCollectedMapper;

    @Autowired
    private BoxAllMapper boxAllMapper;
    /**
     * 收藏一个蜂箱
     * @return
     */
    @Override
    public CommonResult Collect(BoxCollected boxCollected) {
        try{
            BoxCollected box = boxCollectedMapper.selectById(boxCollected.getOpenId());
            if(box!=null){
                box.setBoxId(box.getBoxId()+","+boxCollected.getBoxId());
                boxCollectedMapper.updateById(box);
            }else {
                boxCollectedMapper.insert(boxCollected);
            }
            return new CommonResult("200","收藏成功");
        }catch (Exception e){
            return new CommonResult("400","收藏失败"+e.toString());
        }
    }

    /**
     * 获取已经收藏的蜂箱id
     * @param openId
     * @return
     */
    @Override
    public List<Integer> getBoxIdsCollected(String openId){
        if(boxCollectedMapper.selectCount(new QueryWrapper<BoxCollected>().eq("open_id",openId))>0){
            BoxCollected boxCollected = boxCollectedMapper.selectById(openId);
            return Arrays.asList((Integer[]) ConvertUtils.convert(boxCollected.getBoxId().split(","),Integer.class));
        }else {
            return Collections.EMPTY_LIST;
        }

    }

    /**
     * 删除用户收藏蜂箱id
     * @return
     */
    @Override
    public CommonResult Delete(BoxCollected boxCollected) {
        try{
            List<Integer> boxIdDeleted = new ArrayList<>(Arrays.asList((Integer[]) ConvertUtils.convert(boxCollected.getBoxId().split(","),Integer.class)));//被删除的boxId
            BoxCollected boxCollectedHaved= boxCollectedMapper.selectById(boxCollected.getOpenId());//拥有的boxCollect
            List<Integer> boxIdHaved = new ArrayList<>(Arrays.asList((Integer[]) ConvertUtils.convert(boxCollectedHaved.getBoxId().split(","),Integer.class)));
            boxIdDeleted.forEach(b->{
                if(boxIdHaved.contains(b)){
                    boxIdHaved.remove(Integer.valueOf(b));
                }
            });
            if(boxIdHaved.isEmpty()){
                boxCollectedMapper.deleteById(boxCollected.getOpenId());
                return new CommonResult("200","删除成功");
            }
            String collect = boxIdHaved.stream().map(String::valueOf).collect(Collectors.joining(","));//将数字用逗号连接
            boxCollected.setBoxId(collect);
            boxCollectedMapper.updateById(boxCollected);
            return new CommonResult("200","删除成功");
        }catch (Exception e){
            return new CommonResult("400","删除失败"+e.toString());
        }
    }

}
